package com.example.skillcinema.ui.screens.search

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.example.skillcinema.R
import com.example.skillcinema.databinding.FragmentSearchBinding
import com.example.skillcinema.databinding.FragmentSearchOptionsBinding
import com.example.skillcinema.db.enums.FilmOrderTypes
import com.example.skillcinema.db.enums.FilmTypes
import com.example.skillcinema.ui.screens.main.FilmDetailsFragment
import com.example.skillcinema.ui.screens.search.viewModels.SearchViewModel
import com.google.android.material.slider.RangeSlider

class SearchOptionsFragment : Fragment() {
    private var _binding: FragmentSearchOptionsBinding? = null
    private val binding: FragmentSearchOptionsBinding
        get() = _binding!!
    private val viewModel: SearchViewModel by activityViewModels<SearchViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentSearchOptionsBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (activity as? AppCompatActivity)?.setSupportActionBar(binding.toolbar)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayHomeAsUpEnabled(true)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayShowTitleEnabled(false)
        binding.toolbar.setNavigationOnClickListener {
            findNavController().navigateUp()
        }
        binding.countryTextView.text = viewModel.country?.country
        binding.genreTextView.text = viewModel.country?.country
        binding.seenTextView.text = if (viewModel.isWatched) {
            binding.seenLabel.setImageResource(R.drawable.ic_seen)
            getString(R.string.watched)
        } else {
            binding.seenLabel.setImageResource(R.drawable.ic_not_seen)
            getString(R.string.not_watched)
        }
        with(binding) {
            when (viewModel.filmType) {
                FilmTypes.FILM -> filmRB.isChecked = true
                FilmTypes.TV_SERIES

                    -> serialRB.isChecked = true

                FilmTypes.ALL -> allRB.isChecked = true
                else -> {

                }
            }
            when (viewModel.orderType) {
                FilmOrderTypes.RATING -> rateRB.isChecked = true
                FilmOrderTypes.NUM_VOTE -> popularRB.isChecked = true
                FilmOrderTypes.YEAR -> dateRB.isChecked = true
            }
        }
        viewModel.rate?.let {
            binding.rateRangeSlider.values = listOf(it.first, it.second)
        }
        viewModel.year?.let {
            binding.yearTextView.text = "${it.first}-${it.second}"
        }
        viewModel.country?.let {
            binding.countryTextView.text = it.country
        }
        viewModel.genre?.let {
            binding.genreTextView.text = it.genre
        }
        binding.filmTypeRadioLayout.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.allRB -> viewModel.filmType = FilmTypes.ALL
                R.id.filmRB -> viewModel.filmType = FilmTypes.FILM
                R.id.serialRB -> viewModel.filmType = FilmTypes.TV_SERIES
            }
        }
        binding.sortTypeRadioLayout.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.dateRB -> viewModel.orderType = FilmOrderTypes.YEAR
                R.id.popularRB -> viewModel.orderType = FilmOrderTypes.NUM_VOTE
                R.id.rateRB -> viewModel.orderType = FilmOrderTypes.RATING
            }
        }
        binding.rateRangeSlider.addOnSliderTouchListener(object :
            RangeSlider.OnSliderTouchListener {
            override fun onStartTrackingTouch(slider: RangeSlider) {
                val values = binding.rateRangeSlider.values
            }

            override fun onStopTrackingTouch(slider: RangeSlider) {
                val values = binding.rateRangeSlider.values
                viewModel.rate = values[0] to values[1]

            }
        })
        binding.seenLayout.setOnClickListener {
            viewModel.isWatched = !viewModel.isWatched
            binding.seenTextView.text = if (viewModel.isWatched) {
                binding.seenLabel.setImageResource(R.drawable.ic_seen)
                getString(R.string.watched)
            } else {
                binding.seenLabel.setImageResource(R.drawable.ic_not_seen)
                getString(R.string.not_watched)
            }
        }
        binding.countryLayout.setOnClickListener {
            openCounty()
        }
        binding.genreLayout.setOnClickListener {
            openGenre()
        }
        binding.yearLayout.setOnClickListener {
            openYear()
        }
    }

    private fun openGenre() {
        val bundle: Bundle = Bundle().apply {
            putBoolean(CountryAndGenreSearchFragment.ARG_PARAM1, true)
        }
        findNavController().navigate(
            R.id.action_searchOptionsFragment_to_countryAndGenreSearchFragment,
            bundle
        )
    }

    private fun openCounty() {
        val bundle: Bundle = Bundle().apply {
            putBoolean(CountryAndGenreSearchFragment.ARG_PARAM1, false)
        }
        findNavController().navigate(
            R.id.action_searchOptionsFragment_to_countryAndGenreSearchFragment,
            bundle
        )
    }

    private fun openYear() {
        findNavController().navigate(R.id.action_searchOptionsFragment_to_yearSearchFragment)
    }
}